package com.linkoog.devtools.project_manager.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.ToggleAction;
import com.intellij.openapi.project.Project;
import com.intellij.util.ui.UIUtil;
import com.linkoog.devtools.jdkEx.JdkEx;
import com.linkoog.devtools.project_manager.settings.ProjectManagerSetting;
import com.linkoog.devtools.project_manager.mac_tab.ProjectTabOpertion;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Objects;

/**
 * 强制项目窗口在tab中显示,(已经实现)
 */
public class ToggleForceProjectTabsAction extends ToggleAction {

    public ToggleForceProjectTabsAction() {
        super("Force Project Window In Tabs");
    }

    @Override
    public boolean isSelected(@NotNull AnActionEvent e) {
        final Project project = e.getProject();
        if (project == null) return false;
        return ProjectManagerSetting.getInstance().isForceProjectOneTab();
    }

    @Override
    public void setSelected(@NotNull AnActionEvent e, boolean state) {
        final Project project = e.getProject();
        if (project == null) return;
        ProjectManagerSetting.getInstance().setForceProjectOneTab(state);

        if (state){
            //mergeAllWindows
            Window window = Objects.requireNonNull(UIUtil.getWindow(e.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT)));
            ProjectTabOpertion.mergeAllWindows(window);
        } else {
            ProjectTabOpertion.disperseWindows();
        }
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        Presentation presentation = e.getPresentation();
        if (JdkEx.isTabbingModeAvailable()) {
            presentation.setVisible(true);
        } else {
            presentation.setEnabledAndVisible(false);
        }
        super.update(e);
    }

}

